﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Game
{
    class Penguin : IComparable<Penguin>
    {
        private PictureBox pictureBox1;
        Timer t;
        private System.Drawing.Point dir;
        private DateTime created;
        private SortedSet<Penguin> penguins;
        public Penguin(System.Drawing.Point location, System.Drawing.Point direction, Form1 form,SortedSet<Penguin> collection)
        {
            dir = new System.Drawing.Point((int)(5*direction.X/Math.Sqrt(direction.X*direction.X + direction.Y*direction.Y)),(int)(5*direction.Y/Math.Sqrt(direction.X*direction.X + direction.Y*direction.Y)));
            created = DateTime.Now;
            penguins = collection;

            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();

            this.pictureBox1.BackgroundImage = global::Game.Resource1.olala;
            this.pictureBox1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.pictureBox1.Location = location;
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(103, 176);
            this.pictureBox1.TabIndex = 1;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Show();
            form.Controls.Add(pictureBox1);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            BeginMove();
        }


        public int X
        {
            get
            {
                return pictureBox1.Location.X;
            }
        }

        public int Y
        {
            get
            {
                return pictureBox1.Location.Y;
            }
        }
        public int Width
        {
            get
            {
                return pictureBox1.Width;
            }
        }

        public int Height
        {
            get
            {
                return pictureBox1.Height;
            }
        }

        public void Destroy()
        {
            this.pictureBox1.BackgroundImage = global::Game.Resource1.dead;
        }

        private void Move()
        {
          

            pictureBox1.Location = new System.Drawing.Point(pictureBox1.Location.X + dir.X, pictureBox1.Location.Y + dir.Y);
            if (pictureBox1.Location.X <-40)
            {
                penguins.Remove(this);
                t.Stop();
                t.Dispose();
                pictureBox1.Visible =false;
                pictureBox1 = null;
            }
        }

        private void BeginMove()
        {
            t = new Timer();
            t.Interval = 20;
            t.Tick += new EventHandler(delegate { this.Move(); });
            t.Start();
        }

        public int CompareTo(Penguin obj)
        {
            return created.CompareTo(obj.created);
        }
    }
}
